package com.ronan.imserver.service.impl;

import com.ronan.imserver.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * FileName    : com.ronan.common.service.impl
 * Description :
 *
 * @author : 毒液
 * @version : 1.0
 * Create Date : 2021/2/25 16:17
 **/
@Service
@ConditionalOnClass(RedisTemplate.class)
public class RedisServiceImpl implements RedisService {
    private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public RedisServiceImpl() {
    }

    @Override
    public void set(String key, Object value, long time) {
        this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public void set(String key, Object value) {
        this.redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public Object get(String key) {
        return this.redisTemplate.opsForValue().get(key);
    }

    @Override
    public Boolean del(String key) {
        return this.redisTemplate.delete(key);
    }

    @Override
    public Long del(List<String> keys) {
        return this.redisTemplate.delete(keys);
    }

    @Override
    public Boolean expire(String key, long time) {
        return this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    @Override
    public Long getExpire(String key) {
        return this.redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    @Override
    public Boolean hasKey(String key) {
        return this.redisTemplate.hasKey(key);
    }

    @Override
    public Long incr(String key, long delta) {
        return this.redisTemplate.opsForValue().increment(key, delta);
    }

    @Override
    public Long decr(String key, long delta) {
        return this.redisTemplate.opsForValue().increment(key, -delta);
    }

    @Override
    public Object hGet(String key, String hashKey) {
        return this.redisTemplate.opsForHash().get(key, hashKey);
    }

    @Override
    public Boolean hSet(String key, String hashKey, Object value, long time) {
        this.redisTemplate.opsForHash().put(key, hashKey, value);
        return this.expire(key, time);
    }

    @Override
    public void hSet(String key, String hashKey, Object value) {
        this.redisTemplate.opsForHash().put(key, hashKey, value);
    }

    @Override
    public Map<Object, Object> hGetAll(String key) {
        return this.redisTemplate.opsForHash().entries(key);
    }

    @Override
    public Boolean hSetAll(String key, Map<String, Object> map, long time) {
        this.redisTemplate.opsForHash().putAll(key, map);
        return this.expire(key, time);
    }

    @Override
    public void hSetAll(String key, Map<String, Object> map) {
        this.redisTemplate.opsForHash().putAll(key, map);
    }

    @Override
    public void hDel(String key, Object... hashKey) {
        this.redisTemplate.opsForHash().delete(key, hashKey);
    }

    @Override
    public Boolean hHasKey(String key, String hashKey) {
        return this.redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    @Override
    public Long hIncr(String key, String hashKey, Long delta) {
        return this.redisTemplate.opsForHash().increment(key, hashKey, delta);
    }

    @Override
    public Long hDecr(String key, String hashKey, Long delta) {
        return this.redisTemplate.opsForHash().increment(key, hashKey, -delta);
    }

    @Override
    public Set<Object> sMembers(String key) {
        return this.redisTemplate.opsForSet().members(key);
    }

    @Override
    public Long sAdd(String key, Object... values) {
        return this.redisTemplate.opsForSet().add(key, values);
    }

    @Override
    public Long sAdd(String key, long time, Object... values) {
        Long count = this.redisTemplate.opsForSet().add(key, values);
        this.expire(key, time);
        return count;
    }

    @Override
    public Boolean sIsMember(String key, Object value) {
        return this.redisTemplate.opsForSet().isMember(key, value);
    }

    @Override
    public Long sSize(String key) {
        return this.redisTemplate.opsForSet().size(key);
    }

    @Override
    public Long sRemove(String key, Object... values) {
        return this.redisTemplate.opsForSet().remove(key, values);
    }

    @Override
    public Object sPop(String key) {
        return this.redisTemplate.opsForSet().pop(key);
    }

    @Override
    public List<Object> lRange(String key, long start, long end) {
        return this.redisTemplate.opsForList().range(key, start, end);
    }

    @Override
    public Long lSize(String key) {
        return this.redisTemplate.opsForList().size(key);
    }

    @Override
    public Object lIndex(String key, long index) {
        return this.redisTemplate.opsForList().index(key, index);
    }

    @Override
    public Long lPush(String key, Object value) {
        return this.redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public Long lPush(String key, Object value, long time) {
        Long index = this.redisTemplate.opsForList().rightPush(key, value);
        this.expire(key, time);
        return index;
    }

    @Override
    public Long lPushAll(String key, Object... values) {
        return this.redisTemplate.opsForList().rightPushAll(key, values);
    }

    @Override
    public Long lPushAll(String key, Long time, Object... values) {
        Long count = this.redisTemplate.opsForList().rightPushAll(key, values);
        this.expire(key, time);
        return count;
    }

    @Override
    public Long lRemove(String key, long count, Object value) {
        return this.redisTemplate.opsForList().remove(key, count, value);
    }

    @Override
    public Boolean tryLock(String key, long time) {
        Object lockObj = this.get(key);
        if (lockObj != null) {
            log.warn("[尝试着获取锁] 锁已存在 key:{}", key);
            return false;
        } else {
            this.set(key, System.currentTimeMillis(), time);
            return true;
        }
    }

    @Override
    public void unlock(String key) {
        this.del(key);
    }
}
